package com.tuo51.foudation.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.ErrorCode;
import com.tuo51.foudation.model.StatusResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.*;

import static com.tuo51.foudation.constant.CommonConstant.SSO_CALL_ERROR_MSG;
import static com.matt.commons.model.ErrorCode.JSON_STR_ERROR;

/**
 * *******************************
 * Created by Ron.
 * form com.tuo51
 * on 2018/3/7
 * *******************************
 */
@Slf4j
public class JsonValidateUtil {
    public static ObjectMapper objectMapper = new ObjectMapper();


    public static boolean validateLPrepareTool(String prepareTool) {
        try {
            List<Map> mapList = objectMapper.readValue(prepareTool, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("name")) || Objects.isNull(p.get("number")) || Objects.isNull(p.get("imgUrl")))
                    throw new BusinessException(JSON_STR_ERROR);
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "用具准备json格式错误");
        }
        return true;
    }

    public static boolean validateLinkItem(String linkItems) {
        try {
            List<Map> mapList = objectMapper.readValue(linkItems, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("name")) || Objects.isNull(p.get("steps")))
                    throw new BusinessException(JSON_STR_ERROR);
                List<Map> steps = ((List) p.get("steps"));
                for (Map o : steps) {
                    if (Objects.isNull(o.get("teacherSay"))
                            || Objects.isNull(o.get("teacherAction"))
                            || Objects.isNull(o.get("teacherExpression"))
                            || Objects.isNull(o.get("childAnswer"))
                            || Objects.isNull(o.get("teacherBackSay"))
                            || Objects.isNull(o.get("teacherBackAction"))
                            || Objects.isNull(o.get("duration"))
                            || Objects.isNull(o.get("teachTools")))
                        throw new BusinessException(JSON_STR_ERROR);
                    List<Map> teachTools = ((List) o.get("teachTools"));
                    for (Map t : teachTools) {
                        if (Objects.isNull(t.get("name"))
                                || Objects.isNull(t.get("number"))
                                || Objects.isNull(t.get("imgUrl")))
                            throw new BusinessException(JSON_STR_ERROR);
                    }
                }

            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "环节Json格式错误");
        }
        return true;
    }


    public static boolean validateObserverPoint(String observerPoints) {
        try {
            List<Map> mapList = objectMapper.readValue(observerPoints, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("question")) || Objects.isNull(p.get("typeName")) || Objects.isNull(p.get("typeCode")) || Objects.isNull(p.get("options")))
                    throw new BusinessException(JSON_STR_ERROR);
                List<Map> options = ((List) p.get("options"));
                for (Map o : options) {
                    if (Objects.isNull(o.get("title")) || Objects.isNull(o.get("score")))
                        throw new BusinessException(JSON_STR_ERROR);
                }
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "观测点Json格式错误");
        }
        return true;
    }

    public static boolean validateAttachment(String attachments) {
        try {
            List<Map> mapList = objectMapper.readValue(attachments, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("name")) || Objects.isNull(p.get("type")) || Objects.isNull(p.get("url")))
                    throw new BusinessException(JSON_STR_ERROR);
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "附件Json格式错误");
        }
        return true;
    }

    public static boolean validateChildrenCourseStatus(String json) {
        try {
            List<Map> mapList = objectMapper.readValue(json, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("childrenCourseItemId")) || Objects.isNull(p.get("status")))
                    throw new BusinessException(JSON_STR_ERROR);
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "学生课程状态Json格式错误");
        }
        return true;
    }

    public static boolean validateLinkItemsPrepareRecord(String json) {
        try {
            List<Map> mapList = objectMapper.readValue(json, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("remark")) || Objects.isNull(p.get("isConfirmed")))
                    throw new BusinessException(JSON_STR_ERROR);
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "环节备注Json格式错误");
        }
        return true;
    }

    public static String getMSNMessageType(String message){
        try {
            Map map = JsonValidateUtil.objectMapper.readValue(message, Map.class);
            return map.get("Type").toString();
        } catch (IOException e) {
           log.error("MSNMessage Json转换错误");
        }
        return null;
    }

    public static String getMSNMessageObject(String message){
        try {
            Map map = JsonValidateUtil.objectMapper.readValue(message, Map.class);
            return ((Map)((Map)((Map)map.get("MediaWorkflowExecution")).get("Input")).get("InputFile")).get("Object").toString();
        } catch (IOException e) {
            log.error("MSNMessage Json转换错误");
        }
        return null;
    }

    public static boolean validateObservationPointsRecord(String json) {
        try {
            List<Map> mapList = objectMapper.readValue(json, List.class);
            for (Map p : mapList) {
                if (Objects.isNull(p.get("remark")) || Objects.isNull(p.get("isConfirmed")))
                    throw new BusinessException(JSON_STR_ERROR);
            }
        } catch (Exception e) {
            throw new BusinessException(JSON_STR_ERROR, "观测点备注Json格式错误");
        }
        return true;
    }

    public static Map readToMap(String src) {
        Map map;
        try {
            map = JsonValidateUtil.objectMapper.readValue(src, HashMap.class);
        } catch (IOException e) {
            throw new BusinessException(JSON_STR_ERROR, "附件Json格式错误");
        }
        return map;
    }

    public static List readToList(String src, String errorMsg) {
        List map;
        try {
            map = JsonValidateUtil.objectMapper.readValue(src, List.class);
        } catch (IOException e) {
            throw new BusinessException(JSON_STR_ERROR, errorMsg);
        }
        return map;
    }


    public static StatusResponse getResponse(String src) {
        try {
            StatusResponse statusResponse = JsonValidateUtil.objectMapper.readValue(src, StatusResponse.class);
            if (statusResponse.getCode().equals(ErrorCode.UNDEFINED))
                throw new BusinessException(statusResponse.getCode(), SSO_CALL_ERROR_MSG);
            if (!statusResponse.getCode().equals(ErrorCode.SUCCESSFUL))
                throw new BusinessException(statusResponse.getCode(), statusResponse.getMessage());
            return statusResponse;
        } catch (IOException e) {
            throw new BusinessException(JSON_STR_ERROR, SSO_CALL_ERROR_MSG);
        }
    }

    public static String toJson(Object o){
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.UNDEFINED, "json转换出错");
        }
    }

    public static <T> List<T> readListFormJson(String json, Class<T> clazz){
        try {
            JavaType javaType =ModelUtil.objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
            return ModelUtil.objectMapper.readValue(json, javaType);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.UNDEFINED, "json转换出错");
        }
    }
}
